﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using WpfAppMissData.Biz;
using WpfAppMissData.Extensions;
using WpfAppMissData.Log;
using WpfAppMissData.Orm;

namespace WpfAppMissData
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    // ReSharper disable once RedundantExtendsListEntry
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
        }


        private void ButSearchAndReissue_Click(object sender, RoutedEventArgs e)
        {

            try
            {
                ButSearchAndReissue.IsEnabled = false;
                using (var ctx = new datagatewayEntities())
                {
                    var targets = ctx.pi_total_water_port_map.ToList();
                    foreach (var item in targets)
                    {
                        ctx.pi_total_water_port_map.Remove(item);
                    }
                    ctx.SaveChanges();
                    TbLostHourInfo.Text = "删除成功！";
                }

                using (var ctx = new datagatewayEntities())
                {
                    var sources = ctx.v_company_info.ToList();
                    int i = 1;
                    foreach (var item in sources)
                    {
                        if (item.WaterMN != null)
                        {
                            var m = new pi_total_water_port_map
                            {
                                ID = i++,
                                SCMN = item.WaterMN,
                                ZLMN = item.TotalMN
                            };
                            ctx.pi_total_water_port_map.Add(m);
                            ctx.SaveChanges();
                        }
                    }

                    TbLostDayInfo.Text = "添加成功!";
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(typeof (MainWindow), ex);
                TbReissueInfo.Text = ex.Message;
            }
            finally
            {
                ButSearchAndReissue.IsEnabled = true;
            }
          
            
        }

        private void Window_Closing_1(object sender, System.ComponentModel.CancelEventArgs e)
        {
            var result = MessageBox.Show(@"确定关闭程序！", @"提示", MessageBoxButton.YesNo);
            if (result.Equals(MessageBoxResult.No))
            {
                e.Cancel = true;
            }
            else
            {
                //一定要将关联的进程杀死，负责界面没有，任务管理器里还有进程
                AppliactionHelper.KillApplicationSelf();
            }
        }
    }
}
